package com.geek.utils;

import com.geek.base.file.bean.FileBean;
import com.geek.base.file.bean.SftpBean;
import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class SftpUtil {

    /**
     * SFTP 协议主类
     */
    private static ChannelSftp sftp;
    /**
     * SFTP 访问会话
     */
    private static Session session;

    /**
     * 登录sftp服务器
     */
    public static void loginFtpServer(SftpBean sftpBean) throws JSchException {
        JSch jsch = new JSch();
        session = jsch.getSession(sftpBean.getUserName(), sftpBean.getHost(), sftpBean.getPort());
        if (sftpBean.getPassWord() != null) {
            session.setPassword(sftpBean.getPassWord());
        }
        if (sftpBean.getPrivateKey() != null) {
            jsch.addIdentity(sftpBean.getPrivateKey());
        }
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();
        sftp = (ChannelSftp) channel;
    }

    /**
     * 关闭连接 server
     */
    public static void logoutFtpServer() {
        if (sftp != null) {
            if (sftp.isConnected()) {
                sftp.disconnect();
            }
        }
        if (session != null) {
            if (session.isConnected()) {
                session.disconnect();
            }
        }
    }


    /**
     * 删除文件
     */
    public static void rmFile(String filePath, String fileName) throws Exception {
        sftp.cd(filePath);
        sftp.rm(fileName);
    }

    /**
     * 删除文件夹
     */
    public static void rmDir(String dirPath) throws Exception {
        sftp.rmdir(dirPath);
    }

    /**
     * 将输入流的数据上传到sftp作为文件
     */
    public void upload(String basePath, String directory, String sftpFileName, InputStream input) throws SftpException {
        try {
            sftp.cd(basePath);
            sftp.cd(directory);
        } catch (SftpException e) {
            // 目录不存在，则创建文件夹
            String[] dirs = directory.split("/");
            String tempPath = basePath;
            for (String dir : dirs) {
                if (null == dir || "".equals(dir))
                    continue;
                tempPath += "/" + dir;
                try {
                    sftp.cd(tempPath);
                } catch (SftpException ex) {
                    sftp.mkdir(tempPath);
                    sftp.cd(tempPath);
                }
            }
        }
        sftp.put(input, sftpFileName);
    }

    /**
     * 下载文件
     */
    public void download(String directory, String downloadFile, String saveFile)
            throws SftpException, FileNotFoundException {
        if (directory != null && !"".equals(directory)) {
            sftp.cd(directory);
        }
        File file = new File(saveFile);
        sftp.get(downloadFile, new FileOutputStream(file));
    }



    /**
     * 列出目录下的文件
     */
    public static List<String> listFile(String directory) throws SftpException {
        return sftp.ls(directory);
    }

    public static void main(String[] args) throws SftpException, IOException {

    }

}
